package com.umfb.bootstrap.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;

import com.umfb.bootstrap.commons.ResponseResult;
import com.umfb.bootstrap.commons.ReturnedData;
import com.umfb.bootstrap.commons.SentParameters;

import com.umfb.demo.entity.User;
import com.umfb.demo.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gzheng on 17-11-21.
 */
@Controller
@RequestMapping

public class UserSysController {

    @Reference
    IUserService iUserService;

    @RequestMapping(value = "/login")
    private  ModelAndView login(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/login");
        return mv;

    }

    @RequestMapping(value="/index")
    private ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping(value="/userLogin")
    @ResponseBody
    public ResponseResult userLogin(@RequestParam(value = "userName", required = true) String userName,
                                    @RequestParam(value = "password", required = true) String password){
        User user = new User();
        user.setEmail(userName);
        user.setPassword(password);
        User u = iUserService.findUserByUsernameAndPassword(user);
        ResponseResult rslt = new ResponseResult();

        if(u!=null){

            rslt.setState(ResponseResult.STATE_SUC);
        }else {
            rslt.setState(ResponseResult.STATE_ERR);
        }
        return rslt;
    }

    /**
     * 查询用户列表
     * @param param
     * @param phone
     * @param email
     * @return
     */
    @RequestMapping("/findAllUser")
    @ResponseBody
    public ReturnedData<User> findAllUser(SentParameters param, @RequestParam(value = "phone", required = false) String phone,
                                          @RequestParam(value = "email", required = false) String email){
        ReturnedData<User> data = new ReturnedData<>();
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("phone",phone);
        paraMap.put("email",email);

        Map<String, Object> dataMap = iUserService.findAll(param.getPage(), param.getLength(),paraMap);
        data.setData((List)dataMap.get("dataList"));
        data.setDraw(param.getDraw());
        data.setRecordsTotal(Integer.valueOf(String.valueOf(dataMap.get("count"))));
        data.setRecordsFiltered(Integer.valueOf(String.valueOf(dataMap.get("count"))));

        return  data;
    }

    @RequestMapping("/findUserByUserId")
    @ResponseBody
    public ResponseResult findUserByUserId(@RequestParam(value = "userId") String userId){
        ResponseResult result = new ResponseResult();
        User user = iUserService.getUserByUserId(userId);
        result.setResultBean(user);

        return  result;
    }

    //页面跳转

    @RequestMapping("/showUserList")
    public String showMatchMembersList(HttpServletRequest request) {
        String matchId = request.getParameter("matchId");
        if (!StringUtils.isBlank(matchId)) {
            request.setAttribute("matchId", matchId);
        }
        return "/user/showUserList";
    }

    @RequestMapping("/upUser")
    @ResponseBody
    public ResponseResult upMatchApplyInfo(@ModelAttribute User user){
        ResponseResult result = new ResponseResult();
        try{
          iUserService.saveOrUpdate(user);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            return result;
        }
        return  result;
    }
}
